﻿(function ($) { 
    $.fn.setValues = function (options) { 
        var obj = JSON.parse(options);
        $($(this)).each(function () {
            $($(this).children()).each(function () {
                var $this = $(this);
                if ($this.context.type == "text") {
                    $this.val(obj[$this.attr("datakey")]);
                } else if ($this.context.type == "checkbox") {
                    $this.attr("checked", obj[$this.attr("datakey")]);
                } else if ($this.context.type == "radio") {
                    $this = $(this);
                    $checktype = obj[$this.attr("datakey")]; 
                    if ($checktype) {
                        if ($this.val() == "男") {
                            $this.prop("checked", $checktype);
                        }
                        if ($this.val() == "女") {
                            $this.prop("checked", false);
                        }
                    }
                    if (!$checktype) {
                        if ($this.val() == "女") {
                            $this.prop("checked", true);
                        }
                        else {
                            $this.prop("checked", $checktype);
                        }
                    }
                } else if ($this.context.type == "select-one") {
                    $this = $(this);
                    $nationtype = obj[$this.attr("datakey")]; 
                    $this.find("option[value='" + $nationtype + "']").attr("selected", true);
                }
            });
        });  
    }
    $.fn.getValues = function () { 
        var objValues = new Object();
        $($(this)).each(function () {
            $($(this).children()).each(function () {
                var $this = $(this);
                if ($this.context.type == "text") {
                    objValues[$this.attr("datakey")] = $this.val();
                } else if ($this.context.type == "checkbox") {
                    objValues[$this.attr("datakey")] = $this.attr("checked") == "checked" ? true : false;
                } else if ($this.context.type == "radio") {
                    $this = $(this);
                    if ($this.prop("checked")) {
                        if ($this.val() == "男") {
                            objValues[$this.attr("datakey")] = true;
                        } else {
                            objValues[$this.attr("datakey")] = false;
                        }
                    }
                } else if ($this.context.type == "select-one") {
                    objValues[$this.attr("datakey")] = $this.val();
                }
            });
        }); 
        return JSON.stringify(objValues);
    }
    $.fn.searchParams = function () { } 
})(jQuery);